using System;
using System.Collections.Generic;
using System.Text;
using MVCFramework.Models.Filters;
using MVCFramework.Models;

namespace MVCFramework.View
{
    public class DecoratorViewState : ViewState
    {   
        public ViewState coreState;

        protected DecoratorViewState()
        {
         //   System.Diagnostics.Debug.Assert(coreState != null, "Core ViewState Should Be Setted Before leaving contructor ");
        }
        public DecoratorViewState(ViewState state)
        {
            this.coreState = state;
        }

        public override IModel Model
        {
            get
            {
                return coreState.Model;
            }
            set
            {
                coreState.Model = value;
            }
        }

        public override double[] getPoint(double[] coordinates)
        {
            return coreState.getPoint(coordinates);
        }

        public override void DispalyView()
        {
            coreState.DispalyView();
        }
    }

    public class FilteredViewState : DecoratorViewState 
    {
        protected CompositeFilter compositeFilter = new CompositeFilter();

        protected FilteredViewState()
        {

        }

        public FilteredViewState(ViewState state):base ( state )
        {

        }
        public FilteredViewState(ViewState state, IFilter filter)
            : this(state)
        {
            this.AddFilter(filter);     
        }


        public void AddFilter(IFilter filter)
        {
            this.compositeFilter.Add(filter);
        }
        public void RemoveFilter(IFilter filter)
        {
            if (compositeFilter.Contains(filter))
                this.compositeFilter.Remove(filter);
        }
        public void ClearFilters()
        {
            this.compositeFilter.Clear();
        }

        protected virtual  void InitilizeFilter()
        {
            compositeFilter.IsActivated = true;
            ((FilterDecorator)Model).Filter = compositeFilter;
        }

        IModel decoratedModel;
        public override IModel Model
        {
            get
            {
                return GetFilteredModel();
            }

            set
            {
                _model = value;
                coreState.Model = GetFilteredModel();
            }
        }

        private IModel GetFilteredModel()
        {
            if (_model == null) return null;
            if (decoratedModel == null)
            {
                compositeFilter.IsActivated = true;
                decoratedModel = new FilterDecorator(this._model,compositeFilter );
                InitilizeFilter();

            }
            return decoratedModel;
        }

        public static ViewState CreateFilteredState(ViewState view)
        {
            return new FilteredViewState(view);
        }

        public override void DispalyView()
        {
            coreState.DispalyView();
        }
    }
}
